package com.jhx.common.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.jhx.common.R;

/**
 * 通用dialog
 *
 * @author jack 2017-05-08 17:52
 */
public class CommonDialog extends Dialog {
    private TextView mTitleText;
    private TextView mMessageText;
    private TextView mConfirmText;
    private TextView mCancelText;
    private OnClickListener mOnClickListener;

    public CommonDialog(Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View contentView = View.inflate(context, R.layout.dialog_common, null);
        setContentView(contentView);
        mTitleText =   findViewById(R.id.text_title);
        mCancelText =  findViewById(R.id.text_cancel);
        mMessageText = findViewById(R.id.text_message);
        mConfirmText = findViewById(R.id.text_confirm);
        mConfirmText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnClickListener != null) {
                    mOnClickListener.onConfirmClick(view);
                    dismiss();
                }
            }
        });
        mCancelText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnClickListener != null) {
                    mOnClickListener.onCancelClick(view);
                    dismiss();
                }
            }
        });
    }

    public void setTitle(String text) {
        if (!TextUtils.isEmpty(text)) {
            mTitleText.setVisibility(View.VISIBLE);
            mTitleText.setText(text);
        }
    }

    public void setMessage(String text) {
        if (!TextUtils.isEmpty(text)) {
            mMessageText.setVisibility(View.VISIBLE);
            mMessageText.setText(text);
        }
    }

    public void setConfirmBtn(String text) {
        if (!TextUtils.isEmpty(text)) {
            setConfirmBtn(text, getContext().getResources().getColor(R.color.colorPrimary));
        }
    }

    public void setConfirmBtn(String text, int color) {
        if (!TextUtils.isEmpty(text)) {
            mConfirmText.setText(text);
            mConfirmText.setTextColor(color);
        }
    }

    public TextView getTitleText() {
        return mTitleText;
    }

    public TextView getMessageText() {
        return mMessageText;
    }

    public TextView getConfirmText() {
        return mConfirmText;
    }

    public TextView getCancelText() {
        return mCancelText;
    }

    public void setCancelBtn(String text) {
        if (!TextUtils.isEmpty(text)) {
            setCancelBtn(text, getContext().getResources().getColor(R.color.colorPrimary));
        }
    }

    public void setCancelBtn(String text, int color) {
        if (!TextUtils.isEmpty(text)) {
            mCancelText.setText(text);
            mCancelText.setTextColor(color);
        }
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onConfirmClick(View view);

        void onCancelClick(View view);
    }

}
